<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>继承</title>
</head>
<body>

    
</body>
<script type="text/javascript">
    // 1. 原型链继承
    function Supper(){
        this.supPrp = 'supper property'
    }
    Supper.prototype.showSupProp = function(){
        console.log(this.supProp)
    }

    function Sub(){
        this.subProp = 'sub property'
    }
    // 继承的关键
    Sub.prototype = new Supper() // 子类的原型对象指向父类的实例对象
    Sub.property.constructor = Sub // 修正子类实例的构造函数指向
    Sub.prototype.showSubProp = function(){
        console.log(this.subProp)
    }

    var sub1 = new Sub()
    sub1.showSupProp()  // 可以访问父类的方法
    sub1.showSubProp()


    // 2. 借用构造函数继承
    function Person(name,age){
        this.name = name
        this.age = age
    }
    Person.prototype.setName = function(name){
        this.name = name
    }

    function Student(name, age, price){
        // 借用父类构造函数继承属性，就相当于简写了下面两行的赋值
        Person.call(this, name, age)  // 相当于 this.Person(name, age)
        // this.name = name
        // this.age = age
        this.price = price
    }
    Student.prototype.setName = function(name){
        this.name = name
    }
    var s = new Student('Tom', 23, 18000)
    console.log(s)


    // 3. 组合继承
    function Person(name, age){
        this.name = name
        this.age = age
    }
    Person.prototype.setName = function(name){
        this.name = name
    }

    function Student(name, age, price){
        Person.call(this, name, age)  // 借用Person构造函数继承属性
        this.price = price
    }
    Student.prototype = new Person() // 原型链继承Person的方法
    Student.prototype.constructor = Student
    Student.prototype.setPrice = function(price){
        this.price = price
    }

    var stu = new Student('Bob', 22, 16000)
    stu.setName('Jack')
    stu.setPrice(19000)
    console.log(stu)


</script>
</html>